/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Carrinho;
import model.Produto;
import model.ProdutoManager;
import model.Usuario;
import model.UsuarioManager;

/**
 *
 * @author root
 */
@WebServlet(name = "ServletLogin", urlPatterns = {"/ServletLogin"})
public class ServletLogin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        String acao = request.getParameter("acao");
        
        if(acao.equals("novoCadastro")) //usuario que quer criar um novo cadastro na pagina index.jsp
        {            
            RequestDispatcher dispatcher = request.getRequestDispatcher("ServletCadastro?acao=novoCadastro");
            dispatcher.forward(request, response);
        }
        else //usuario pagina index para logar
        {  
            HttpSession session = request.getSession();       

            String login = request.getParameter("email");
            String senha = request.getParameter("senha");
            
            UsuarioManager userM = new UsuarioManager();
            ProdutoManager prodM = new ProdutoManager();
            
            //verifica se o usuario existe
            Usuario usuario = userM.getUsuario(login);
            
            if(usuario != null)//se existe o usuario, cria o carrinho e seta usuario e carrinho na secao
            {
                if(usuario.getSenha().equals(senha))
                {
                    Carrinho carrinho = new Carrinho(usuario.getLogin(), usuario.getNome());
                    session.setAttribute("usuario", usuario); //seta usuario na sessao
                    session.setAttribute("carrinho", carrinho);  //seta carrinho na sessao              
                    ArrayList<Produto> produtos = prodM.getAllProdutos();
                    session.setAttribute("productsList", produtos);
                            
                    RequestDispatcher dispatcher = request.getRequestDispatcher("home.jsp");
                    dispatcher.forward(request, response);
                }
                else
                {
                    RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?acao=Senha incorreta");
                    dispatcher.forward(request, response);
                }
            }
            else
            {
                RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?acao=Usuario nao encontrado");
                dispatcher.forward(request, response);
            }
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
